import 'dart:math' as math;

import 'package:flutter/material.dart';

class BlankToolBar extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return BlankToolBarState();
  }
}

class BlankToolBarState extends State<BlankToolBar>{
  /// 响应空白处的焦点的Node
  FocusNode blankNode = FocusNode();
  ///焦点Map
  Map<String,dynamic> nodeMap = {};
  ///用户名文本框控制器
  TextEditingController nameController = TextEditingController();
  ///密码文本框控制器
  TextEditingController pwdController = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose(){
    ///销毁页面时
    ///循环焦点Map数组
    ///移除焦点监听
    for(Map map in nodeMap.values){
      FocusNode focusNode = map['node'];
      focusNode.removeListener(focusNodeListener);
    }
    super.dispose();
  }

  ///空焦点监听
  Future<Null> focusNodeListener() async {
    // 用async的方式实现这个listener
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text('登录'),),
        body: Stack(
          children: <Widget>[
            Positioned(top: 0,left: 0,bottom: 0,right: 0,
              child: GestureDetector(
                onTap: blankClicked,
                child: createBody(),
              ),
            ),
            Positioned(top: 0,left: 0,bottom: 0,right: 0,
              child: Column(
                children: <Widget>[
                  Flexible(child: Container()),
                  createToolBar()
                ],
              ),
            ),

          ],
        )
    );
  }


  /// 点击了空白的地方
  void blankClicked(){
    /// 点击空白页面关闭键盘
    FocusScope.of(context).requestFocus(blankNode);
  }
  ///返回当前编辑焦点节点的Map
  Map? currentEditingFocusNode(){
    print("");
    ///循环焦点Map数组
    ///如果当前焦点拥有焦点，则返回Map
    for(Map map in nodeMap.values){
      FocusNode focusNode = map['node'];
      if(focusNode.hasFocus){
        return map;
      }
    }
    return null;
  }

  ///焦点节点的索引
  void focusNodeAtIndex(int selectIndex){
    ///循环焦点Map数组
    for(Map map in nodeMap.values){
      ///获取焦点节点的索引
      int index = map['index'];
      ///如果焦点节点的索引 == 当前的选择索引
      if(selectIndex == index){
        ///则为当前焦点节点获取索引
        FocusNode focusNode = map['node'];
        focusNode.requestFocus();
        return;
      }
    }
  }

  ///创建键盘TooBar
  Widget createToolBar(){
    return Container(
      height: 40,
      color: const Color(0xffeeeeee),
      padding: const EdgeInsets.only(left: 10,right: 10),
      child: Row(
        children: <Widget>[
          // InkWell(child: preBtn,onTap: (){},),
          const SizedBox(width: 40,),
          // InkWell(child: nextBtn,onTap: (){},),
          Flexible(child: Container(),),
          InkWell(onTap: blankClicked, child: const Text('关闭',style: TextStyle(color: Colors.blue,))),
        ],
      ),
    );
  }
  // 创建展示内容
  Widget createBody(){
    return ListView(
      padding: const EdgeInsets.only(left: 20,right: 20),
      children: <Widget>[
        const SizedBox(height: 30),
        createInputText(nameController,hint: '请输入用户名',icon: Icons.people),
        const SizedBox(height: 30),
        createInputText(pwdController,hint: '请输入密码',icon: Icons.power,obscureText:true),
        const SizedBox(height: 30),
        ElevatedButton(onPressed: checkLogin,child: const Text('登录'),)
      ],
    );
  }

  // 创建输入行
  Widget createInputText(TextEditingController controller,{obscureText: false,required String hint,required IconData icon}){
    // controller的唯一code
    String key = controller.hashCode.toString();
    Map? map = nodeMap[key];
    FocusNode focusNode;
    if(map == null){
      focusNode = FocusNode();
      map = {};
      map['node'] = focusNode;
      map['index'] = nodeMap.length;
      focusNode.addListener(focusNodeListener);
      nodeMap[key] = map;
    }else{
      focusNode = map['node'];
    }
    // 输入框
    TextField textField = TextField(
        controller: controller,
        keyboardType: TextInputType.text,
        decoration: InputDecoration(
          contentPadding: EdgeInsets.all(10.0),
          hintText: hint,
        ),
        obscureText: obscureText,
        focusNode: focusNode,
        keyboardAppearance:Brightness.light
    );

    List<Widget> rowList = [];
    // 输入框前的提示图标
    rowList.add(SizedBox(width: 10));
    rowList.add(Icon(icon));
    // 输入框
    rowList.add(Expanded(child: textField));


    return Row(children: rowList);
  }

  // 点击登录处理
  void checkLogin(){
    print(nameController.text);
    print(pwdController.text);
  }

}